# DataFlare Makefile

# Variables
BINARY_NAME=dataflare
BUILD_DIR=./bin
DIST_DIR=./dist
COVER_PROFILE=coverage.out
COVER_HTML=coverage.html
GOFILES=$(shell find . -type f -name "*.go" -not -path "./vendor/*")
GOPACKAGES=$(shell go list ./... | grep -v /vendor/)
GOFMT=gofmt -s

# Go commands
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOINSTALL=$(GOCMD) install
GOMOD=$(GOCMD) mod

# Build flags
LDFLAGS=-ldflags "-X github.com/louloulin/dataflare/internal/version.Version=`git describe --tags --always --dirty=-dev`"
GOBUILDFLAGS=$(LDFLAGS) -v

# OS and architecture detection
OS := $(shell uname -s | tr '[:upper:]' '[:lower:]')
ARCH := $(shell uname -m)
ifeq ($(ARCH),x86_64)
	ARCH := amd64
endif

# Default target
.PHONY: all
all: clean build test

# Build the project
.PHONY: build
build:
	@echo "Building $(BINARY_NAME)..."
	@mkdir -p $(BUILD_DIR)
	$(GOBUILD) $(GOBUILDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/dataflare

# Clean build artifacts
.PHONY: clean
clean:
	@echo "Cleaning..."
	@$(GOCLEAN)
	@rm -rf $(BUILD_DIR)
	@rm -rf $(DIST_DIR)
	@rm -f $(COVER_PROFILE)
	@rm -f $(COVER_HTML)

# Run tests
.PHONY: test
test:
	@echo "Running tests..."
	$(GOTEST) -v -race $(GOPACKAGES)

# Run tests with coverage
.PHONY: coverage
coverage:
	@echo "Running tests with coverage..."
	$(GOTEST) -v -race -coverprofile=$(COVER_PROFILE) $(GOPACKAGES)
	$(GOCMD) tool cover -html=$(COVER_PROFILE) -o $(COVER_HTML)
	@echo "Coverage report generated at $(COVER_HTML)"

# Run tests with verbose output
.PHONY: test-verbose
test-verbose:
	$(GOTEST) -v -race $(GOPACKAGES)

# Run the application
.PHONY: run
run: build
	@echo "Running $(BINARY_NAME)..."
	$(BUILD_DIR)/$(BINARY_NAME)

# Run with config file
.PHONY: run-config
run-config: build
	@echo "Running $(BINARY_NAME) with config..."
	$(BUILD_DIR)/$(BINARY_NAME) --config configs/config.yaml

# Development mode with auto-reload
.PHONY: dev
dev:
	@echo "Running in development mode..."
	@if ! command -v air > /dev/null; then \
		echo "Installing air..."; \
		go install github.com/cosmtrek/air@latest; \
	fi
	air

# Format code
.PHONY: fmt
fmt:
	@echo "Formatting code..."
	@$(GOFMT) -w $(GOFILES)

# Lint code
.PHONY: lint
lint:
	@echo "Linting code..."
	@if ! command -v golangci-lint > /dev/null; then \
		echo "Installing golangci-lint..."; \
		go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; \
	fi
	golangci-lint run

# Install dependencies
.PHONY: deps
deps:
	@echo "Installing dependencies..."
	$(GOMOD) download
	$(GOMOD) tidy

# Setup development environment
.PHONY: setup-dev
setup-dev: deps
	@echo "Setting up development environment..."
	@if ! command -v golangci-lint > /dev/null; then \
		echo "Installing golangci-lint..."; \
		go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; \
	fi
	@if ! command -v air > /dev/null; then \
		echo "Installing air..."; \
		go install github.com/cosmtrek/air@latest; \
	fi
	@if ! command -v swag > /dev/null; then \
		echo "Installing swag..."; \
		go install github.com/swaggo/swag/cmd/swag@latest; \
	fi
	@echo "Development environment setup complete"

# Build for all supported platforms
.PHONY: dist
dist: clean
	@echo "Building for multiple platforms..."
	@mkdir -p $(DIST_DIR)
	
	# Linux
	GOOS=linux GOARCH=amd64 $(GOBUILD) $(GOBUILDFLAGS) -o $(DIST_DIR)/$(BINARY_NAME)-linux-amd64 ./cmd/dataflare
	GOOS=linux GOARCH=arm64 $(GOBUILD) $(GOBUILDFLAGS) -o $(DIST_DIR)/$(BINARY_NAME)-linux-arm64 ./cmd/dataflare
	
	# macOS
	GOOS=darwin GOARCH=amd64 $(GOBUILD) $(GOBUILDFLAGS) -o $(DIST_DIR)/$(BINARY_NAME)-darwin-amd64 ./cmd/dataflare
	GOOS=darwin GOARCH=arm64 $(GOBUILD) $(GOBUILDFLAGS) -o $(DIST_DIR)/$(BINARY_NAME)-darwin-arm64 ./cmd/dataflare
	
	# Windows
	GOOS=windows GOARCH=amd64 $(GOBUILD) $(GOBUILDFLAGS) -o $(DIST_DIR)/$(BINARY_NAME)-windows-amd64.exe ./cmd/dataflare
	
	@echo "Distribution builds completed in $(DIST_DIR)/"

# Install the application
.PHONY: install
install: build
	@echo "Installing $(BINARY_NAME)..."
	cp $(BUILD_DIR)/$(BINARY_NAME) $(GOPATH)/bin/

# Generate API documentation
.PHONY: docs
docs:
	@echo "Generating API documentation..."
	@if ! command -v swag > /dev/null; then \
		echo "Installing swag..."; \
		go install github.com/swaggo/swag/cmd/swag@latest; \
	fi
	swag init -g cmd/dataflare/server/api.go -o docs/api

# Build Docker image
.PHONY: docker
docker:
	@echo "Building Docker image..."
	docker build -t dataflare:latest .

# Run Docker container
.PHONY: docker-run
docker-run:
	@echo "Running Docker container..."
	docker run -p 12300:12300 dataflare:latest

# Verify all files have been committed
.PHONY: verify-commit
verify-commit:
	@if [ -n "$(shell git status --porcelain)" ]; then \
		echo "Error: There are uncommitted changes:"; \
		git status --short; \
		exit 1; \
	fi

# Create a new release
.PHONY: release
release: verify-commit dist
	@echo "Creating release..."
	@read -p "Enter release version (e.g., v1.0.0): " version; \
	git tag -a $$version -m "Release $$version"; \
	git push origin $$version; \
	echo "Release $$version created"

# Run shell with development environment
.PHONY: shell
shell:
	@echo "Starting development shell..."
	@bash --rcfile <(echo '. ~/.bashrc; PS1="[dataflare] \w > "')

# Help target
.PHONY: help
help:
	@echo "DataFlare Makefile targets:"
	@echo "  all          - Clean, build, and test"
	@echo "  build        - Build the application"
	@echo "  clean        - Clean build artifacts"
	@echo "  test         - Run tests"
	@echo "  coverage     - Run tests with coverage report"
	@echo "  run          - Build and run the application"
	@echo "  run-config   - Run with config file"
	@echo "  dev          - Run in development mode with auto-reload"
	@echo "  fmt          - Format code"
	@echo "  lint         - Lint code"
	@echo "  deps         - Install dependencies"
	@echo "  setup-dev    - Setup development environment"
	@echo "  dist         - Build for all supported platforms"
	@echo "  install      - Install the application"
	@echo "  docs         - Generate API documentation"
	@echo "  docker       - Build Docker image"
	@echo "  docker-run   - Run Docker container"
	@echo "  release      - Create a new release"
	@echo "  shell        - Start development shell"
	@echo "  help         - Show this help message"